/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uber.netbsd.community;





import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import uber.netbsd.community.dao.PersonBuilder;
import uber.netbsd.community.dao.PersonManager;
import uber.netbsd.community.dao.Role;


/**
 * <p>StartupListener class used to initialize and database settings
 * and populate any application-wide drop-downs.
 * <p/>
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
public class StartupListener extends Loggered implements ServletContextListener {

   

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public void contextInitialized(ServletContextEvent event) {
     //   log.debug("Initializing context...");

        ServletContext context = event.getServletContext();

        PersonManager pm  = PersonManager.getInstance();


        /**
         *  We check  if database was already created
         */
        if (!pm.getAllPeople().isEmpty())
            return;


        /**
         * create some users
         */

          pm.saveOrUpdate(PersonBuilder.getInstance()
                .addFirstName("Alex")
                .addLastName("the Developer")
                .addUsername("alex")
                .addPassword("login")
                .addEmail("me@alex.0x08.org")
                .addRole(Role.ROLE_ADMIN)
                .get());




        pm.saveOrUpdate(PersonBuilder.getInstance()
                .addFirstName("Darth")
                .addLastName("Vader")
                .addUsername("dvader")
                .addPassword("vader")
                .addEmail("dvader@darkstar.gov")
                .addRole(Role.ROLE_MANAGER)
                .get());



        pm.saveOrUpdate(PersonBuilder.getInstance()
                .addFirstName("Marylin")
                .addLastName("Manson")
                .addUsername("manson")
                .addPassword("manson")
                .addEmail("manson@mtv.com")
                .addRole(Role.ROLE_USER)
                .get());


    }


    /**
     * Shutdown servlet context (currently a no-op method).
     *
     * @param servletContextEvent The servlet context event
     */
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        //LogFactory.release(Thread.currentThread().getContextClassLoader());
        //Commented out the above call to avoid warning when SLF4J in classpath.
        //WARN: The method class org.apache.commons.logging.impl.SLF4JLogFactory#release() was invoked.
        //WARN: Please see http://www.slf4j.org/codes.html for an explanation.
    }
}
